<?php
namespace Curl;
class CurlHttpRequest
{
    use InitCurl;

    /**
     * @Notes: curl post 请求
     * @User: chenping
     * @email: 375129701@qq.com
     * @param $url
     * @param array $param
     * @param array $option
     * @return mixed
     */
    public function curlHttpPost($url,$param=[],$option=[])
    {
        //请求地址
        if(!isset($option[CURLOPT_URL]))
        {
            $option[CURLOPT_URL] = $url;
        }
        //post 请求设置
        if(!isset($option[CURLOPT_POST]))
        {
            $option[CURLOPT_POST] = true;
        }
        //参数选项
        if(!isset($option[CURLOPT_POSTFIELDS]))
        {

            $option[CURLOPT_POSTFIELDS] = $param;//$this->httpBuildQuery($param);
        }
        //合并数据
        if(!empty($option))
        {
            $this->setConfig($option);
        }

        $ch = curl_init();
        curl_setopt_array($ch,$this->curlConfigOption);
        $output = curl_exec($ch);

        curl_close($ch);

        if($ch==false)
        {
            return curl_error($ch);
        }else{
            return $output;
        }
    }

    /**
     * @Notes: get 请求
     * @User: chenping
     * @email: 375129701@qq.com
     * @param $url
     * @param array $param
     * @param array $option
     * @return mixed|string
     */
    public function curlHttpGet($url,$param=[],$option=[])
    {
        $ch = curl_init();
        if(!empty($option))
        {
            $this->setConfig($option);
        }
        if($param){
            $url= $url."?".$this->httpBuildQuery($param);
        }
        curl_setopt($ch,CURLOPT_URL,$url);

        if($this->curlConfigOption)
        {
            foreach ($this->curlConfigOption as $k=> $v){
                curl_setopt($ch,$k,$v);
            }
        }

        $output = curl_exec($ch);

        if(curl_error($ch))
        {
            return curl_error($ch);
        }else{
            return $output;
        }
    }

    /**
     * @Notes: curl Delete 请求
     * @User: chenping
     * @email: 375129701@qq.com
     * @param $url
     * @param array $param
     * @param array $option
     * $method [get,post]
     * @return mixed
     */
    public function curlHttpDelete($url,$param=[],$option=[],$method='get')
    {
        // 请求设置
        if(!isset($option[CURLOPT_CUSTOMREQUEST]))
        {
            $option[CURLOPT_CUSTOMREQUEST] = "DELETE";
        }
        //参数选项
        if(!isset($option[CURLOPT_POSTFIELDS]) && $method=="post")
        {
            $option[CURLOPT_POSTFIELDS] = $this->httpBuildQuery($param);
        }else{
            $url= $url."?".$this->httpBuildQuery($param);
        }

        //请求地址
        if(!isset($option[CURLOPT_URL]))
        {
            $option[CURLOPT_URL] = $url;
        }
        //合并数据
        if(!empty($option))
        {
            $this->setConfig($option);
        }
        $ch = curl_init();
        curl_setopt_array($ch,$this->curlConfigOption);
        $output = curl_exec($ch);
        curl_close($ch);
        if($ch==false)
        {
            return curl_error($ch);
        }else{
            return $output;
        }
    }

    /**
     * @Notes: curl Delete 请求
     * @User: chenping
     * @email: 375129701@qq.com
     * @param $url
     * @param array $param
     * @param array $option
     * $method [get,post]
     * @return mixed
     */
    public function curlHttpPut($url,$param=[],$option=[],$method='get')
    {
        // 请求设置
        if(!isset($option[CURLOPT_PUT]))
        {
            $option[CURLOPT_PUT] = true;
        }
        //参数选项
        if(!isset($option[CURLOPT_POSTFIELDS]) && $method=="post")
        {
            $option[CURLOPT_POSTFIELDS] = $this->httpBuildQuery($param);
        }else{
            if($param){
                $url= $url."?".$this->httpBuildQuery($param);
            }
        }
        //请求地址
        if(!isset($option[CURLOPT_URL]))
        {
            $option[CURLOPT_URL] = $url;
        }
        //合并数据
        if(!empty($option))
        {
            $this->setConfig($option);
        }
        $ch = curl_init();
        curl_setopt_array($ch,$this->curlConfigOption);
        $output = curl_exec($ch);
        curl_close($ch);
        if($ch==false)
        {
            return curl_error($ch);
        }else{
            return $output;
        }
    }

    public function post_json_data($url, $data_string) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json; charset=utf-8',
                'Content-Length: ' . strlen($data_string))
        );
        ob_start();
        curl_exec($ch);
        $return_content = ob_get_contents();
        ob_end_clean();
        $return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        return array('code'=>$return_code, 'result'=>$return_content);
    }


}